/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：SyncSettingController.java
 * History:
 *         2012-6-15: Initially created, Chrise.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.base.SyncObject;
import com.xteam.ecsystem.base.SyncType;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.ecsystem.utils.ConstantLocation;

/**
 * 同步设置控制层。
 * @author Chrise
 */
@Controller
public class SyncSettingController {
	
	@Autowired
	private ConfigManager cfgManager;
	
	@Autowired
	private SystemLogService logService;
	
	/**
	 * 获取同步对象。
	 * @author Chrise 2012-6-18
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getSyncObject.do")
	public void getSyncObject(HttpServletRequest request, HttpServletResponse response) throws IOException {
		SyncObject so = this.cfgManager.getSyncObject();
		JSONObject result = JSONObject.fromObject(so);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 同步设置。
	 * @author Chrise 2012-6-18
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/syncSetting.do")
	public void syncSetting(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONObject object = JSONObject.fromObject(data);		
		JSONObject result = new JSONObject();
		
		String error = this.cfgManager.syncSetting(object.getInt("syncType"), object.getString("service"));
		result.put("message", error);
		
		switch (SyncType.valueOf(object.getInt("syncType"))) {
			case SYSCFG:
				this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_SYSCFG_MGR, 
						"syncSetting", ConstantLocation.LOG_SYNC_SETTING, "SysSystemConfig");
				break;
			case REQCFG:
				this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_SYSCFG_MGR, 
						"syncSetting", ConstantLocation.LOG_SYNC_SETTING, "SysRequestConfig");
				break;
			case WEBCFG:
				this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_SYSWEBCFG, 
						"syncSetting", ConstantLocation.LOG_SYNC_SETTING, "SysWebConfig");
				break;
			default:
				break;
		}
		
		response.getWriter().write(result.toString());
	}
}
